<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html><head><title>QWaitCondition Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
td.postheader { font-family: sans-serif }
tr.address { font-family: sans-serif }
body { background: #ffffff; color: black; }
</style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">&#160;&#160;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&#160;&#183; <a href="classes.html"><font color="#004faf">All Classes</font></a>&#160;&#183; <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QWaitCondition Class Reference<br /><sup><sup>[<a href="qtcore.html">QtCore</a> module]</sup></sup></h1><p>The QWaitCondition class provides a condition variable for
synchronizing threads. <a href="#details">More...</a></p>

<h3>Methods</h3><ul><li><div class="fn" /><b><a href="qwaitcondition.html#QWaitCondition">__init__</a></b> (<i>self</i>)</li><li><div class="fn" />bool <b><a href="qwaitcondition.html#wait">wait</a></b> (<i>self</i>, QMutex&#160;<i>mutex</i>, int&#160;<i>msecs</i>&#160;=&#160;ULONG_MAX)</li><li><div class="fn" />bool <b><a href="qwaitcondition.html#wait-2">wait</a></b> (<i>self</i>, QReadWriteLock&#160;<i>readWriteLock</i>, int&#160;<i>msecs</i>&#160;=&#160;ULONG_MAX)</li><li><div class="fn" /><b><a href="qwaitcondition.html#wakeAll">wakeAll</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qwaitcondition.html#wakeOne">wakeOne</a></b> (<i>self</i>)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QWaitCondition class provides a condition variable for
synchronizing threads.</p>
<p>QWaitCondition allows a thread to tell other threads that some
sort of condition has been met. One or many threads can block
waiting for a QWaitCondition to set a condition with <a href="qwaitcondition.html#wakeOne">wakeOne</a>() or <a href="qwaitcondition.html#wakeAll">wakeAll</a>(). Use <a href="qwaitcondition.html#wakeOne">wakeOne</a>() to wake one randomly
selected condition or <a href="qwaitcondition.html#wakeAll">wakeAll</a>() to wake them all.</p>
<p>For example, let's suppose that we have three tasks that should
be performed whenever the user presses a key. Each task could be
split into a thread, each of which would have a <a href="qthread.html#run">run()</a> body like this:</p>
<pre class="cpp">
 forever {
     mutex<span class="operator">.</span>lock();
     keyPressed<span class="operator">.</span>wait(<span class="operator">&amp;</span>mutex);
     do_something();
     mutex<span class="operator">.</span>unlock();
 }
</pre>
<p>Here, the <tt>keyPressed</tt> variable is a global variable of
type QWaitCondition.</p>
<p>A fourth thread would read key presses and wake the other three
threads up every time it receives one, like this:</p>
<pre class="cpp">
 forever {
     getchar();
     keyPressed<span class="operator">.</span>wakeAll();
 }
</pre>
<p>The order in which the three threads are woken up is undefined.
Also, if some of the threads are still in <tt>do_something()</tt>
when the key is pressed, they won't be woken up (since they're not
waiting on the condition variable) and so the task will not be
performed for that key press. This issue can be solved using a
counter and a <a href="qmutex.html">QMutex</a> to guard it. For
example, here's the new code for the worker threads:</p>
<pre class="cpp">
 forever {
     mutex<span class="operator">.</span>lock();
     keyPressed<span class="operator">.</span>wait(<span class="operator">&amp;</span>mutex);
     <span class="operator">+</span><span class="operator">+</span>count;
     mutex<span class="operator">.</span>unlock();

     do_something();

     mutex<span class="operator">.</span>lock();
     <span class="operator">-</span><span class="operator">-</span>count;
     mutex<span class="operator">.</span>unlock();
 }
</pre>
<p>Here's the code for the fourth thread:</p>
<pre class="cpp">
 forever {
     getchar();

     mutex<span class="operator">.</span>lock();
     <span class="comment">// Sleep until there are no busy worker threads</span>
     <span class="keyword">while</span> (count <span class="operator">&gt;</span> <span class="number">0</span>) {
         mutex<span class="operator">.</span>unlock();
         sleep(<span class="number">1</span>);
         mutex<span class="operator">.</span>lock();
     }
     keyPressed<span class="operator">.</span>wakeAll();
     mutex<span class="operator">.</span>unlock();
 }
</pre>
<p>The mutex is necessary because the results of two threads
attempting to change the value of the same variable simultaneously
are unpredictable.</p>
<p>Wait conditions are a powerful thread synchronization primitive.
The <a href="threads-waitconditions.html">Wait Conditions</a>
example shows how to use QWaitCondition as an alternative to
<a href="qsemaphore.html">QSemaphore</a> for controlling access to
a circular buffer shared by a producer thread and a consumer
thread.</p>
<hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QWaitCondition" />QWaitCondition.__init__ (<i>self</i>)</h3><p>Constructs a new wait condition object.</p>


<h3 class="fn"><a name="wait" />bool QWaitCondition.wait (<i>self</i>, <a href="qmutex.html">QMutex</a>&#160;<i>mutex</i>, int&#160;<i>msecs</i>&#160;=&#160;ULONG_MAX)</h3><p>Releases the locked <i>mutex</i> and waits on the wait
condition. The <i>mutex</i> must be initially locked by the calling
thread. If <i>mutex</i> is not in a locked state, this function
returns immediately. If <i>mutex</i> is a recursive mutex, this
function returns immediately. The <i>mutex</i> will be unlocked,
and the calling thread will block until either of these conditions
is met:</p>
<ul>
<li>Another thread signals it using <a href="qwaitcondition.html#wakeOne">wakeOne</a>() or <a href="qwaitcondition.html#wakeAll">wakeAll</a>(). This function will
return true in this case.</li>
<li><i>time</i> milliseconds has elapsed. If <i>time</i> is
<tt>ULONG_MAX</tt> (the default), then the wait will never timeout
(the event must be signalled). This function will return false if
the wait timed out.</li>
</ul>
<p>The mutex will be returned to the same locked state. This
function is provided to allow the atomic transition from the locked
state to the wait state.</p>
<p><b>See also</b> <a href="qwaitcondition.html#wakeOne">wakeOne</a>() and <a href="qwaitcondition.html#wakeAll">wakeAll</a>().</p>


<h3 class="fn"><a name="wait-2" />bool QWaitCondition.wait (<i>self</i>, <a href="qreadwritelock.html">QReadWriteLock</a>&#160;<i>readWriteLock</i>, int&#160;<i>msecs</i>&#160;=&#160;ULONG_MAX)</h3><p>Releases the locked <i>readWriteLock</i> and waits on the wait
condition. The <i>readWriteLock</i> must be initially locked by the
calling thread. If <i>readWriteLock</i> is not in a locked state,
this function returns immediately. The <i>readWriteLock</i> must
not be locked recursively, otherwise this function will not release
the lock properly. The <i>readWriteLock</i> will be unlocked, and
the calling thread will block until either of these conditions is
met:</p>
<ul>
<li>Another thread signals it using <a href="qwaitcondition.html#wakeOne">wakeOne</a>() or <a href="qwaitcondition.html#wakeAll">wakeAll</a>(). This function will
return true in this case.</li>
<li><i>time</i> milliseconds has elapsed. If <i>time</i> is
<tt>ULONG_MAX</tt> (the default), then the wait will never timeout
(the event must be signalled). This function will return false if
the wait timed out.</li>
</ul>
<p>The <i>readWriteLock</i> will be returned to the same locked
state. This function is provided to allow the atomic transition
from the locked state to the wait state.</p>
<p>This function was introduced in Qt 4.4.</p>
<p><b>See also</b> <a href="qwaitcondition.html#wakeOne">wakeOne</a>() and <a href="qwaitcondition.html#wakeAll">wakeAll</a>().</p>


<h3 class="fn"><a name="wakeAll" />QWaitCondition.wakeAll (<i>self</i>)</h3><p>Wakes all threads waiting on the wait condition. The order in
which the threads are woken up depends on the operating system's
scheduling policies and cannot be controlled or predicted.</p>
<p><b>See also</b> <a href="qwaitcondition.html#wakeOne">wakeOne</a>().</p>


<h3 class="fn"><a name="wakeOne" />QWaitCondition.wakeOne (<i>self</i>)</h3><p>Wakes one thread waiting on the wait condition. The thread that
is woken up depends on the operating system's scheduling policies,
and cannot be controlled or predicted.</p>
<p>If you want to wake up a specific thread, the solution is
typically to use different wait conditions and have different
threads wait on different conditions.</p>
<p><b>See also</b> <a href="qwaitcondition.html#wakeAll">wakeAll</a>().</p>
<address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt&#160;4.9.1 for X11</td><td align="center" width="50%">Copyright &#169; <a href="http://www.riverbankcomputing.com">Riverbank&#160;Computing&#160;Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2011</td><td align="right" width="25%">Qt&#160;4.8.0</td></tr></table></div></address></body></html>